<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.keyword.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('picbook:appupgrade:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('picbook:appupgrade:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
      <el-form-item label="客户端" style="margin-left: 60px;">
        <el-select v-model="dataForm.keyword.userAgent" style="width: 120px;" @change="getDataList(1)">
          <el-option
            v-for="item in userAgentOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        type="index"
        width="50"
        label="序号"
        align="center">
      </el-table-column>
      <el-table-column
        prop="userAgent"
        header-align="center"
        align="center"
        label="客户端" width="70">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="app名称" width="120">
      </el-table-column>
      <!-- <el-table-column
        prop="appCode"
        header-align="center"
        align="center"
        width="120"
        label="app分类">
      </el-table-column> -->
      <el-table-column
        prop="version"
        header-align="center"
        align="center"
        width="120"
        label="app版本号">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        min-width="100"
        :show-overflow-tooltip="true"
        label="更新标题">
      </el-table-column>
      <el-table-column
        prop="upgradeInfo"
        header-align="center"
        :show-overflow-tooltip="true"
        label="更新说明">
      </el-table-column>
      <!--<el-table-column-->
      <!--prop="md5"-->
      <!--header-align="center"-->
      <!--align="center"-->
      <!--label="md5">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="force"
        header-align="center"
        align="center"
        width="80"
        label="强制更新">
        <template slot-scope="scope">
          {{scope.row.force==1?'是':'否'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="apkUrl"
        header-align="center"
        :show-overflow-tooltip="true"
        label="apk的url">
      </el-table-column>
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          {{scope.row.state==1?"上架":"下架"}}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id, scope.row.state)">修改</el-button>
          <el-button type="text" size="small" @click="modifyState(scope.row.id, scope.row.state==0?'1':'0')">{{scope.row.state==1?"下架":"上架"}}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id, scope.row.state)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './appupgrade-add-or-update'

  export default {
    data () {
      return {
        userAgentOptions: [{id: '', name: '全部'},{id: 'android', name: 'android'},{id: 'ios', name: 'ios'}],
        dataForm: {
          keyword: {
            key: '',
            userAgent: ''
          }
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },

    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/picbook/appupgrade/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id, state) {
        // 只有下架才能修改
        if (state) {
          this.$message('只有下架状态才能修改，请先下架')
          return
        }
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 上下架
      modifyState (id,state) {
        this.$http({
          url: this.$http.adornUrl('/picbook/appupgrade/modifyState'),
          method: 'post',
          data: this.$http.adornData({
            'id': id,
            'state': state
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 删除
      deleteHandle (id, state) {
        // 只有下架才能删除
        if (state) {
          this.$message('只有下架状态才能删除，请先下架')
          return
        }
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        // this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        this.$confirm(`确定[${id ? '删除' : '批量删除'}]?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/picbook/appupgrade/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      /*用js限制字数，超出部分以省略号...显示*/
      limitNumber (text, maxNumber) {
        var ret = ''
        if (text) {
          if (text.length <= maxNumber) {
            ret = text
          } else {
            ret = text.substr(0, maxNumber) + '……'
          }
        }
        return ret
      }
    }
  }
</script>
